home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlcomp / codegen.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  123 lines

  1.     /***************************************************************\
  2.     *                                *
  3.     *    codegen.c - routines to generate appropriate ADL    *
  4.     *    stack machine instructions.                *
  5.     *    Copyright 1987 by Ross Cunniff.                *
  6.     *                                *
  7.     \***************************************************************/
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "adltypes.h"
  12. #include "virtmem.h"
  13. #include "adldef.h"
  14.  
  15. address
  16.     numcode = 1;        /* Number of bytes of code written    */
  17.  
  18. extern struct pagetab
  19.     codetab;            /* Code paging table.            */
  20.  
  21.     /***************************************************************\
  22.     *                                *
  23.     *    oldcode( addr, opcode, opnd ) - Emits instruction    *
  24.     *    opcode opnd at address addr.  Compresses the instr.    *
  25.     *    if possible.                        *
  26.     *                                *
  27.     \***************************************************************/
  28.  
  29. address
  30. oldcode( addr, opcode, opnd )
  31. address
  32.     addr;
  33. int16
  34.     opnd;
  35. char
  36.     opcode;
  37. {
  38.     int16
  39.     num;
  40.  
  41.     switch( opcode ) {
  42.     case PUSH :
  43.         if( (opnd >= -127) && (opnd <= 0) ) {
  44.         opcode = PUSHN | (char)(opnd & 0x07F);
  45.         num = 1;
  46.         }
  47.         else if( (opnd >= -1024) && (opnd <= 1023) ) {
  48.         opcode = PUSHS | (char)((opnd >> 8) & 0x03F);
  49.         opnd &= 0x0FF;
  50.         num = 2;
  51.         }
  52.         else
  53.         num = 3;
  54.        break;
  55.     case PUSHARG :
  56.     case PUSHLOCL :
  57.     case CALL :
  58.         opcode |= (char)(opnd & 0x01F);
  59.     case NOP :
  60.     case POP :
  61.     case PUSHME :
  62.     case RET :
  63.         num = 1;
  64.         break;
  65.     case JMP :
  66.     case JMPZ : 
  67.     case FILEN :
  68.     case LINEN :
  69.         num = 3;
  70.         break;
  71.     }
  72.     vm_put8( opcode, (int32)(addr++), &codetab );
  73.     switch( num ) {
  74.     case 1 :
  75.         break;
  76.     case 2 :
  77.         vm_put8( (char)(opnd & 0x0ff), (int32)addr, &codetab );
  78.         break;
  79.     case 3 :
  80.         vm_put16( (int16)(opnd & 0x0ffff), (int32)addr, &codetab );
  81.         break;
  82.     }
  83.     return (address)num;
  84. }
  85.  
  86.  
  87.     /***************************************************************\
  88.     *                                *
  89.     *    newcode( opcode, opnd ) - emit instruction opcode opnd    *
  90.     *    at the current address in the code file.        *
  91.     *                                *
  92.     \***************************************************************/
  93.  
  94. address
  95. newcode( opcode, opnd )
  96. char
  97.     opcode;
  98. int16
  99.     opnd;
  100. {
  101.     address
  102.     nc_save;
  103.  
  104.     nc_save = numcode;
  105.     numcode += oldcode( numcode, opcode, opnd );
  106.     return nc_save;
  107. }
  108.  
  109.  
  110.     /***************************************************************\
  111.     *                                *
  112.     *    currcode() - returns the current code address.        *
  113.     *                                *
  114.     \***************************************************************/
  115.  
  116. address
  117. currcode()
  118. {
  119.     return numcode;
  120. }
  121.  
  122. /*** EOF codegen.c ***/
  123.